import React from 'react';
import Authentication from '../../Component/Authentication';
import Drone from '../../Component/Drone';
import "./index.css";
import AuthenticationServer from "../../Server/Authentication";
import UserCheck from '../../utils/UserCheck';
import HttpUtil from '../../utils/HttpUtil';
import CommonUtil from '../../utils/Common';

export default class CA extends React.Component{
    constructor(props){
        super();
        this.state={
            isDrone:true,
            showDrone:"",
            showCA:"none",
            drones:[],
            caList:[]
        };

        let index=0;
        // 查询无人机认证
        AuthenticationServer.SelectAerocraftList(UserCheck.GetUserID())
        .then(response=>{
            // console.log("查询无人机认证");
            let drones=[];
            response.data.forEach(i => {
                let src=HttpUtil.GetResourceURL(i.aerocraftImgUrl);
                drones.push(
                    <Drone onClick={e=>this.DeleteDrone(i.aerocraftId)} key={index++} id={i.aerocraftId} type={i.aerocraftType}
                    src={src} title={i.aerocraftTitle} desc1={"型号："+i.aerocraftModel} desc2={"SN号："+i.aerocraftSn}/>
                )
            });
            this.setState({
                drones:drones
            })
        });

        // 查询飞行执照认证
        AuthenticationServer.SelectCertificateList(UserCheck.GetUserID())
        .then(response=>{
            // console.log("查询飞行执照认证");
            let caList=[];
            response.data.forEach(i => {
                let src=HttpUtil.GetResourceURL(i.certificateImgJustUrl);
                caList.push(
                    <Drone key={index++} onClick={e=>this.DeleteCA(i.certificateId)}  id={i.certificateId} title={i.certificateType} 
                    src={src} desc1={"证书编号："+i.certificateNumber} desc2={"证书有效期："+i.certificateDate}/>
                );
            });
            this.setState({
                caList:caList
            })

            // console.log(response);
        });
    }
    changeSelect(isDrone){
        this.setState({
            isDrone:isDrone
        },()=>{
            if(this.state.isDrone){
                this.setState({
                    showDrone:"",
                    showCA:"none"
                },()=>{
                    CommonUtil.ResizeIFrame();
                });
            }
            else{
                this.setState({
                    showCA:"",
                    showDrone:"none"
                },()=>{
                    CommonUtil.ResizeIFrame();
                });
            }
        })
    }

    DeleteDrone(id){
        AuthenticationServer.DeleteAerocraft(id)
        .then(response=>{
            let node=document.getElementById(id);
            node.style.display="none";
        });
    }
    DeleteCA(id){
        AuthenticationServer.DeleteCertificate(id)
        .then(response=>{
            let node=document.getElementById(id);
            node.style.display="none";
        });
    }
    render()
    {
        return(
            <div>
                <div>
                    <Authentication changeTab={e=>this.changeSelect(e)}/>
                </div>
                <div className="CA_Drone_Container" style={{display:this.state.showDrone}}>
                   {this.state.drones}
               </div>
                <div className="CA_Drone_Container" style={{display:this.state.showCA}}>
                    {this.state.caList}
                </div>
            </div>
        );
    }
}